package com.white.commonlib.widget;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;

import java.lang.reflect.Field;

/**
 * Created by ytf on 2017/3/31 031.
 * Description: 改进的SwipeRefreshLayout，使用必须要导入v4包，as默认导入
 * 调用{@link SwipeRefreshLayout#setRefreshing(boolean s)}就可以直接回调onRefresh方法，主要用于首次刷新
 */

public class NewSwipeRefresh extends SwipeRefreshLayout
{
    public NewSwipeRefresh(Context context)
    {
        super(context);
    }

    public NewSwipeRefresh(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    OnRefreshListener l;
    boolean isListenerSetted;
    boolean isReflectGetted;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev)
    {
        try
        {
            if(freField != null)
                mRefreshing = (boolean) freField.get(this);
        } catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
        if(mRefreshing)
        {
            return false;
        }
        else
        {
            return super.dispatchTouchEvent(ev);
        }
    }

    boolean mRefreshing;
    Field freField;
    boolean myIsRefreshing;

    @Override
    public void setRefreshing(boolean refreshing)
    {
        super.setRefreshing(refreshing);
        if(refreshing)
        {
            if(l == null)
            {
                if(!isReflectGetted)
                {
                    if(isListenerSetted)
                    {
                        try
                        {
                            Class clazz = Class.forName("android.support.v4.widget.SwipeRefreshLayout");
                            if(clazz != null)
                            {
                                Field f = clazz.getDeclaredField("mListener");
                                freField = clazz.getDeclaredField("mRefreshing");
                                f.setAccessible(true);
                                freField.setAccessible(true);
                                l = (OnRefreshListener) f.get(this);
                                if(l != null)
                                {
                                    l.onRefresh();
                                    isReflectGetted = true;
                                }else
                                {
                                    isListenerSetted = false;
                                }
                            }
                        } catch (ClassNotFoundException e)
                        {
                            e.printStackTrace();
                        } catch (NoSuchFieldException e)
                        {
                            e.printStackTrace();
                        } catch (IllegalAccessException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
            }else
            {
                l.onRefresh();
            }
        }
    }

    @Override
    public void setOnRefreshListener(OnRefreshListener listener)
    {
        super.setOnRefreshListener(listener);
        isListenerSetted = true;
    }
}
